package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.activity.BaseActivity;
import com.jdy.haoduoaiteacher.model.LePrivateChannelItem;
import com.jdy.haoduoaiteacher.view.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

@SuppressLint("InflateParams")
public class PrivateChannelMenuItemAdapter extends BaseAdapter {

  private BaseActivity mContext;
  private ArrayList<LePrivateChannelItem> mList;
  private LayoutInflater mInflter;
  DisplayImageOptions options;

  public PrivateChannelMenuItemAdapter(BaseActivity context,
      ArrayList<LePrivateChannelItem> list) {
    this.mContext = context;
    this.mList = list;
    mInflter = LayoutInflater.from(mContext);

    // 用 displayImage 加载图片 的 配置参数
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    // 创建默认的ImageLoader配置参数
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs() // 打印 log 信息
        .build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LePrivateChannelItem itemVo = mList.get(position);
    ViewHolder holder;

    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflter.inflate(R.layout.girdviewitem, null);
      holder.itemImg = (RoundImageView) convertView
          .findViewById(R.id.imageView);
      holder.itemName = (TextView) convertView.findViewById(R.id.textView);
      holder.tagtext = (TextView) convertView.findViewById(R.id.tag);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.itemName.setText(itemVo.album_name);
    holder.tagtext.setText(itemVo.tags);
    ImageLoader.getInstance().displayImage(itemVo.album_cover, holder.itemImg,
        options);
    return convertView;
  }

  private static class ViewHolder {
    private TextView itemName;
    private RoundImageView itemImg;
    private TextView tagtext;
  }

}
